package com.qiguliuxing.dts.db.service;

import com.github.pagehelper.PageHelper;
import com.qiguliuxing.dts.db.dao.IordersMapper;
import com.qiguliuxing.dts.db.domain.Iorders;
import com.qiguliuxing.dts.db.domain.Iorders.Column;
import com.qiguliuxing.dts.db.domain.IordersExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
public class IorderService {
    private static final Logger logger = LoggerFactory.getLogger(IorderService.class);
    Column[] columns = new Column[] { Column.id, Column.date, Column.packageId, Column.orderCount1, Column.orderCount2, Column.orderCount3, Column.batchNumber, Column.splitActivity,Column.remainVolume,Column.splitVolume,Column.concentration,Column.remainVolume, Column.measureTime, Column.productionQuantity, Column.productionActivity,Column.remainActivity,
            Column.drumA,Column.drumB,Column.drumC,Column.leadB,Column.leadC,Column.leadE,Column.sodiumHippurate, Column.notes};
    @Resource
    private IordersMapper iorderMapper;


    /**
     * 获取热卖商品
     *
     * @param offset
     * @param limit
     * @return
     */
    public List<Iorders> queryById(int offset, int limit) {
        IordersExample example = new IordersExample();
/*        example.or().andIseffectiveEqualTo(true);*/
        example.setOrderByClause("overdue desc");
        PageHelper.startPage(offset, limit);

        return iorderMapper.selectByExampleSelective(example, columns);
    }

    /**
     * 获取新品上市
     *
     * @param offset
     * @param limit
     * @return
     */
    public List<Iorders> queryByNew(int offset, int limit) {
        IordersExample example = new IordersExample();
        example.setOrderByClause("date desc");
        PageHelper.startPage(offset, limit);

        return iorderMapper.selectByExampleSelective(example, columns);
    }

    /**
     * 获取分类下的商品
     *
     * @param catList
     * @param offset
     * @param limit
     * @return
     */
    public List<Iorders> queryByid(List<Integer> catList, int offset, int limit) {
        IordersExample example = new IordersExample();
     /*   example.or().andIdIn(catList).andIseffectiveEqualTo(true);*/
        example.setOrderByClause("date  desc");
        PageHelper.startPage(offset, limit);

        return iorderMapper.selectByExampleSelective(example, columns);
    }

    /*/**
     * 获取分类下的商品
     *
     * @param catId
     * @param offset
     * @param limit
     * @return
     */
    /*
     */
    /*
    public List<Iorders> queryByCategory(Integer catId, int offset, int limit) {
        IordersExample example = new IordersExample();
        example.or().andCategoryIdEqualTo(catId).andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(offset, limit);

        return iorderMapper.selectByExampleSelective(example, columns);
    }
*/

  /*  public List<Iorders> querySelective(int id, String instrumentNumber, String instrumentLink, int instrumentType,
                                      int offset, int limit, String sort,String order) {
        IordersExample example = new IordersExample();
        IordersExample.Criteria criteria1 = example.createCriteria();
//        IordersExample.Criteria criteria2 = example.createCriteria();

        if (!StringUtils.isEmpty(id) && id != 0) {
            criteria1.andIdEqualTo(id);
//            criteria2.andIdEqualTo(id);
        }
        if (StringUtil.isNotEmpty(instrumentNumber)) {
            criteria1.andinstrumentNumberEqualTo(instrumentNumber);
//            criteria2.andinstrumentNumberEqualTo(instrumentNumber);
        }
        if (!StringUtils.isEmpty(instrumentLink)) {
            criteria1.andinstrumentLinkEqualTo(instrumentLink);
//            criteria2.andinstrumentLinkEqualTo(instrumentLink);
        }
        if (!StringUtils.isEmpty(instrumentType)) {
            criteria1.andinstrumentTypeEqualTo(instrumentType);
//            criteria2.andinstrumentTypeEqualTo(instrumentType);
        }

        criteria1.andIseffectiveEqualTo(true);
//        criteria2.andIseffectiveEqualTo(true);

//        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
//            example.setOrderByClause(sort + " " + order);
//        }

        PageHelper.startPage(offset, limit);
        return iorderMapper.selectByExample(example);
    }*

   */
/*开发一个起始日期-结束日期查询功能*/
    public List<Iorders> querySelective(String startTime,String endTime,String batchNumber, Integer page, Integer limit, String sort,
                                      String order) {
        IordersExample example = new IordersExample();
        IordersExample.Criteria criteria = example.createCriteria();

       /* if (!StringUtils.isEmpty(instrumentNumber)) {
            criteria.andinstrumentNumberEqualTo(instrumentNumber);
        }*/
//        if (!StringUtils.isEmpty(name)) {
//            criteria.andNameLike("%" + name + "%");
//        }
//        criteria.andDeletedEqualTo(false);
//
//        if (brandIds != null && brandIds.size() > 0) {
//            criteria.andBrandIdIn(brandIds);
//        }
    /*    if (!StringUtils.isEmpty(instrumentNumber)) {
            criteria.andInstrumentNumberLike("%" + instrumentNumber + "%");
        }
        if (!StringUtils.isEmpty(instrumentName)) {
            criteria.andInstrumentNameLike("%" + instrumentName + "%");
        }
        if (!StringUtils.isEmpty(userDepartments)) {
            criteria.andUserDepartmentsLike("%" + userDepartments + "%");
        }*/
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        if (!startTime.isEmpty() && !endTime.isEmpty()){
            criteria.andBatchNumberBetween(startTime,endTime);
        }
        if (!StringUtils.isEmpty(batchNumber)) {
            criteria.andBatchNumberEqualTo(batchNumber);
        }

            PageHelper.startPage(page, limit);
        return iorderMapper.selectByExample(example);
    }

    /**
     * 获取某个文档,包含完整信息
     *
     * @param id
     * @return
     */
    public Iorders findById(Integer id) {
        IordersExample example = new IordersExample();
        /*example.or().andidEqualTo(id).andDeletedEqualTo(false);*/
        example.or().andIdEqualTo(id);
        /* return goodsMapper.selectOneByExampleWithBLOBs(example);*/
        return iorderMapper.selectOneByExampleSelective(example);
    }

    /**
     * 根据文档编号找到商品
     * @param instrumentNumber
     * @return
     */
    public Iorders findByinstrumenttype(String instrumentNumber) {
        IordersExample example = new IordersExample();
        /*example.or().andGoodsSnEqualTo(goodsSn).andDeletedEqualTo(false);*/
      /*  example.or().andInstrumentNameEqualTo(instrumentNumber).andIseffectiveEqualTo(false);*/
        return iorderMapper.selectOneByExampleSelective(example);
    }

    /**
     * 获取某个商品信息，仅展示相关内容
     *
     * @param id
     * @return
     */
    public Iorders findByIdVO(Integer id) {
        IordersExample example = new IordersExample();
     /*   example.or().andIdEqualTo(id).andIseffectiveEqualTo(true);*/
        return iorderMapper.selectOneByExampleSelective(example, columns);
    }

    public Iorders findBySnVO(int instrumentType) {
        IordersExample example = new IordersExample();
       /* example.or().andInstrumentTypeEqualTo(instrumentType).andIseffectiveEqualTo(true);*/
        return iorderMapper.selectOneByExampleSelective(example, columns);
    }

    /**
     * 获取所有在售物品总数
     *
     * @return
     */
    public Integer queryOnSale() {
        IordersExample example = new IordersExample();
/*        example.or().andIseffectiveEqualTo(true);*/
        return (int) iorderMapper.countByExample(example);
    }

    public int updateById(Iorders instrument) {
     /*   instrument.setUpdatetime(LocalDate.now());*/
        return iorderMapper.updateByPrimaryKeySelective(instrument);
    }

    public void deleteById(Integer id) {
        /*iorderMapper.logicalDeleteByPrimaryKey(id);*/
    }

    public void add(Iorders instrument) {
        /* instrument.setAddTime(""LocalDate.now()"");*/
        /*instrument.setUpdateTime(LocalDateTime.now());*/
        iorderMapper.insertSelective(instrument);
    }

    /**
     * 获取所有物品总数，包括在售的和下架的，但是不包括已删除的商品
     * 获取所有文档总数。
     * @return
     */
    public int count() {
        IordersExample example = new IordersExample();
        example.or();
        return (int) iorderMapper.countByExample(example);
    }

/*    public List<Integer> getCatIds(Integer brandId, String keywords, Boolean isHot, Boolean isNew) {
        IordersExample example = new IordersExample();
        IordersExample.Criteria criteria1 = example.or();
        IordersExample.Criteria criteria2 = example.or();

        if (!StringUtils.isEmpty(brandId)) {
            criteria1.andBrandIdEqualTo(brandId);
            criteria2.andBrandIdEqualTo(brandId);
        }
        if (!StringUtils.isEmpty(isNew)) {
            criteria1.andIsNewEqualTo(isNew);
            criteria2.andIsNewEqualTo(isNew);
        }
        if (!StringUtils.isEmpty(isHot)) {
            criteria1.andIsHotEqualTo(isHot);
            criteria2.andIsHotEqualTo(isHot);
        }
        if (!StringUtils.isEmpty(keywords)) {
            criteria1.andKeywordsLike("%" + keywords + "%");
            criteria2.andNameLike("%" + keywords + "%");
        }
        criteria1.andIsOnSaleEqualTo(true);
        criteria2.andIsOnSaleEqualTo(true);
        criteria1.andDeletedEqualTo(false);
        criteria2.andDeletedEqualTo(false);

        List<Iorders> goodsList = goodsMapper.selectByExampleSelective(example, Column.categoryId);
        List<Integer> cats = new ArrayList<Integer>();
        for (Iorders goods : goodsList) {
            cats.add(goods.getCategoryId());
        }
        return cats;
    }*/

    public boolean checkExistByName(String name) {
        IordersExample example = new IordersExample();
       /* example.or().andInstrumentNameEqualTo(name);*/
        return iorderMapper.countByExample(example) != 0;
    }

    /**
     * 根据店铺，获取店铺对应类别的商品
     * 根据文档类型，获取对应文档类型的文档
     * @param brandId
     * @param i
     * @param related
     * @return
     */
/*    public List<Iorders> queryByBrandId(int bid, int cid, int offset, int limit) {
        IordersExample example = new IordersExample();
        example.or().andBrandIdEqualTo(bid).andCategoryIdEqualTo(cid).andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        example.setOrderByClause("browse desc");
        PageHelper.startPage(offset, limit);

        return iorderMapper.selectByExampleSelective(example, columns);
    }*/

    /**
     * 同类商品，且不同店铺
     *
     * @param brandId
     * @param cid
     * @param i
     * @param limitCid
     * @return
     */
  /*  public List<Iorders> queryByCategoryAndNotSameBrandId(int bid, int cid, int offset, int limit) {
        IordersExample example = new IordersExample();
        example.or().andBrandIdNotEqualTo(bid).andCategoryIdEqualTo(cid).andIsOnSaleEqualTo(true)
                .andDeletedEqualTo(false);
        example.setOrderByClause("browse desc");
        PageHelper.startPage(offset, limit);

        return iorderMapper.selectByExampleSelective(example, columns);
    }*/
}
